<template>
  <div class = "login">
      <div class = "login_context">
    <!--头部图片-->
          <div class = "login_logo">
            <img src="../assets/logo.png">
          </div>
    <!--表单-->
    <el-form :model="loginForm" :rules="loginFromRul" ref="loginFormRel" class="login_box">
      <el-form-item  prop="username">
        <el-input prefix-icon="el-icon-user" v-model="loginForm.username"></el-input>
      </el-form-item>
      <el-form-item  prop="password">
        <el-input prefix-icon="el-icon-lock" type="password" v-model="loginForm.password"></el-input>
      </el-form-item>
      <el-form class="btns">
        <el-button type="primary" @click="login">登录</el-button>
        <el-button type="info">重置</el-button>
      </el-form>
    </el-form>
      </div>
  </div>
</template>
<script>
  import {login} from "../api/file"
  export default {
    data(){
    return{
      //表单请求数据
      loginForm:{
        username:'admin',
        password:'123456'
      },
      //表单验证规则
      loginFromRul:{
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ],
      }
      }
    },
    methods:{
      //登录
      login(){
        login(this.loginForm).then(res=>{
          console.log(res)
          if(res.data.meta.status !== 200) return this.$message.error('登录失败');
          this.$message.success('登录成功')
          //保持当前的token’
          window.sessionStorage.setItem('token',res.data.data.token);
          //登录成功跳转到home
          this.$router.push('/home')

        }).catch(err=>{
          console.log(err)
          this.$message.error('登录失败');
          // this.$message.error(err);
        })
        //登录之前进行校验
        // this.$refs.loginFormRel.validate(vaild=>{
        //   if((!vaild)) return;
        //    this.$http.post('login',this.loginForm).then(res =>{
        //      console.log(res)
        //     if(res.data.meta.status !== 200) return this.$message.error('登录失败');
        //     this.$message.success('登录成功')
        //      //保持当前的token’
        //      window.sessionStorage.setItem('token',res.data.data.token);
        //     //登录成功跳转到home
        //      this.$router.push('/home')
        //   })
        // })
      }
    }
  }

</script>
<style lang="less" scoped>
  .login{
    height: 100%;
    background-color: whitesmoke;
  }
  .login_context{
      width: 450px;
      height: 300px;
    background-color: honeydew;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);
    border-radius: 10px;
    box-shadow: 0 0 2px 5px #ddd;
  }
  .login_logo{
    width: 150px;
    height: 150px;
    position: absolute;
    top: -75px;
    left: 50%;
    margin-left: -80px;
    border: 1px solid #eee;
    border-radius: 50%;
    background-color: #eee;
    padding: 10px;
    box-shadow: 0 0 3px 2px #fff;
  }
  .login_logo img{
    width: 100%;
    height: 100%;
    border-radius: 50%;
    background-color: beige;
  }
  .login_box{
    width: 100%;
    position: absolute;
    bottom: 0;
    padding: 0 20px;
    box-sizing: border-box;
  }
  .btns{
    display: flex;
    justify-content: flex-end;
  }
</style>
